package cn.cloudplug.friendsrecommend.view;


import cn.cloudplug.friendsrecommend.DDInformationActivity;
import cn.cloudplug.friendsrecommend.LoginActivity;
import cn.cloudplug.friendsrecommend.MyDataActivity;
import cn.cloudplug.friendsrecommend.MyMessageActivity;
import cn.cloudplug.friendsrecommend.MyRaceActivity;
import cn.cloudplug.friendsrecommend.MyScoreActivity;
import cn.cloudplug.friendsrecommend.MyTeamActivity;
import cn.cloudplug.friendsrecommend.R;
import cn.cloudplug.friendsrecommend.SettingActivity;
import cn.cloudplug.friendsrecommend.util.Constant;
import cn.cloudplug.friendsrecommend.util.GetPhotoUtil;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;

public class LeftMenu extends Activity{
	private Activity activity;
	private SlidingMenu slidingMenu;
	public ImageView iv_photo;
	public TextView tv_name;
	public LinearLayout logined;
	public LinearLayout unLogin;
	public GetPhotoUtil change;
	private LeftMenuItemView[] leftMenuItemArray = new LeftMenuItemView[7];
	Class[] classArray = { MyDataActivity.class, MyRaceActivity.class,
			MyScoreActivity.class, MyTeamActivity.class, MyMessageActivity.class,
			DDInformationActivity.class, SettingActivity.class };

	public LeftMenu(final Activity activity) {
		
		this.activity = activity;
		
		View view = View.inflate(activity, R.layout.left_menu, null);
		int screenWidth = activity.getWindowManager().getDefaultDisplay().getWidth();
		int leftSlidingMenuWidth=screenWidth *3 / 5;
		slidingMenu = new SlidingMenu(activity);
		LinearLayout.LayoutParams params=new LayoutParams(leftSlidingMenuWidth, LinearLayout.LayoutParams.MATCH_PARENT);
		view.setLayoutParams(params);
		slidingMenu.setMenu(view);
		slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
		slidingMenu.attachToActivity(activity, SlidingMenu.SLIDING_CONTENT);
		slidingMenu.setMode(SlidingMenu.LEFT);
		slidingMenu.setBehindOffset(screenWidth *2 / 5);
		
		SharedPreferences sp=activity.getSharedPreferences("config", MODE_PRIVATE);
		
		unLogin=(LinearLayout) view.findViewById(R.id.layout_number_unlogin);
		logined=(LinearLayout) view.findViewById(R.id.layout_number_logined);
		if(sp.getBoolean("isLogin", false)){
			unLogin.setVisibility(View.GONE);
			logined.setVisibility(View.VISIBLE);
		}else{
			logined.setVisibility(View.GONE);
			unLogin.setVisibility(View.VISIBLE);
		}
		iv_photo=(ImageView) view.findViewById(R.id.iv_logined_photo);
		tv_name=(TextView) view.findViewById(R.id.tv_logined_name);
		iv_photo.setOnClickListener(listener);
		tv_name.setOnClickListener(listener);
		
		view.findViewById(R.id.tv_reg).setOnClickListener(listener);
		view.findViewById(R.id.tv_login).setOnClickListener(listener);
		view.findViewById(R.id.iv_unlogin_photo).setOnClickListener(listener);

		
		leftMenuItemArray[0] = (LeftMenuItemView) view.findViewById(R.id.leftMenuItem_my_data);
		leftMenuItemArray[1] = (LeftMenuItemView) view.findViewById(R.id.leftMenuItem_my_race);
		leftMenuItemArray[2] = (LeftMenuItemView) view.findViewById(R.id.leftMenuItem_my_score);
		leftMenuItemArray[3] = (LeftMenuItemView) view.findViewById(R.id.leftMenuItem_my_team);
		leftMenuItemArray[4] = (LeftMenuItemView) view.findViewById(R.id.leftMenuItem_my_message);
		leftMenuItemArray[5] = (LeftMenuItemView) view.findViewById(R.id.leftMenuItem_dong_dong);
		leftMenuItemArray[6] = (LeftMenuItemView) view.findViewById(R.id.leftMenuItem_system_setting);

		for (int i = 0; i < leftMenuItemArray.length; i++) {
			final int index = i;
			leftMenuItemArray[i].setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					if(leftMenuItemArray[index].tv_message.isShown()){
						leftMenuItemArray[index].tv_message.setVisibility(View.GONE);
					}
					Intent intent = new Intent(activity, classArray[index]);
					activity.startActivity(intent);
				}
			});
		}
	}

	private OnClickListener listener=new OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.tv_login:
			case R.id.iv_unlogin_photo:
				loginOrRegist("login");
				break;
			case R.id.tv_reg:
				loginOrRegist("reg");
				break;
			case R.id.iv_logined_photo:
				change=new GetPhotoUtil(activity, Constant.PIC_HEAD_PHOTO);
				change.picPhoto();
			}
		}
	};


	public void updateUI(){
		for (int i = 0; i < leftMenuItemArray.length; i++) {
			String str=i+"";
			if(str!=null){
				leftMenuItemArray[i].tv_message.setVisibility(View.VISIBLE);
				leftMenuItemArray[i].tv_message.setText(i+"");
			}
		}
	}
	

	public void showMenu() {
		slidingMenu.showMenu();
	}

	private void loginOrRegist(String action) {
		Intent intent=new Intent();
		intent.setClass(activity, LoginActivity.class);
		intent.putExtra("action", action);
		activity.startActivity(intent);
		activity.overridePendingTransition(R.anim.activity_in, R.anim.activity_out);
	}

	

}
